package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type DeviceTemplateEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	SubsetName   string // 子集名称
	TemplateType string // 模板类型
	TemplateName string // 模板名称

	TemplateParam map[string]interface{} // 配置参数
	ExtendParam   map[string]interface{} // 扩展参数
}

type deviceTemplatePo struct {
	Id            int64                  `json:"id" db:"id"`                        // 对象id
	CreateTime    int64                  `json:"createTime" db:"create_time"`       // 创建时间
	UpdateTime    int64                  `json:"updateTime" db:"update_time"`       // 更新时间
	Manufacturer  string                 `json:"manufacturer" db:"manufacturer"`    // 设备厂商名称
	DeviceType    string                 `json:"deviceType" db:"device_type"`       // 设备类型名
	SubsetName    string                 `json:"subsetName" db:"subset_name"`       // 模板名称
	TemplateType  string                 `json:"templateType" db:"template_type"`   // 模板名称
	TemplateName  string                 `json:"templateName" db:"template_name"`   // 模板名称
	TemplateParam map[string]interface{} `json:"templateParam" db:"template_param"` // 配置参数
	ExtendParam   map[string]interface{} `json:"extendParam" db:"extend_param"`     // 扩展参数
}

func (e *DeviceTemplateEntity) Type() string {
	return "DeviceTemplateEntity"
}

func (e *DeviceTemplateEntity) TableName() string {
	return "tb_device_template"
}

func (e *DeviceTemplateEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceTemplateEntity) Instance() *DeviceTemplateEntity {
	return &DeviceTemplateEntity{
		BaseEntity:    new(commEntity.BaseEntity),
		TemplateParam: make(map[string]interface{}),
		ExtendParam:   make(map[string]interface{}),
	}
}

func (e *DeviceTemplateEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceTemplateEntity) NewPo() interface{} {
	return &deviceTemplatePo{
		TemplateParam: make(map[string]interface{}),
		ExtendParam:   make(map[string]interface{}),
	}
}

func (e *DeviceTemplateEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceTemplateEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Manufacturer)
	p = append(p, e.DeviceType)
	p = append(p, e.SubsetName)
	p = append(p, e.TemplateType)
	p = append(p, e.TemplateName)
	return p
}

func (e *DeviceTemplateEntity) BuildByJson(jsn *string) error {
	var po deviceTemplatePo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceTemplateEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceTemplateEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceTemplatePo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.TemplateParam = po.TemplateParam
	ptr.ExtendParam = po.ExtendParam
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.SubsetName = po.SubsetName
	ptr.TemplateType = po.TemplateType
	ptr.TemplateName = po.TemplateName
}

func (e *DeviceTemplateEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceTemplatePo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.TemplateParam = ptr.TemplateParam
	po.ExtendParam = ptr.ExtendParam
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.SubsetName = ptr.SubsetName
	po.TemplateType = ptr.TemplateType
	po.TemplateName = ptr.TemplateName

	return &po
}
